package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.SysEntitlement;
import com.fjwt.gz.service.mapper.SysEntitlementMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统权限表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2020-06-13
 */
@Service
public class SysEntitlementService extends ServiceImpl<SysEntitlementMapper, SysEntitlement> {


    /**
     * 新增路由
     * @param sysEntitlement
     * @return
     */
    public Integer add(SysEntitlement sysEntitlement) {
        if (StringUtils.isEmpty(sysEntitlement.getEntId())) {
            throw new BizException("路由ID不能为空");
        }
        if (StringUtils.isEmpty(sysEntitlement.getEntName())) {
            throw new BizException("路由名称不能为空");
        }
        if (StringUtils.isEmpty(sysEntitlement.getEntType())) {
            throw new BizException("请选择菜单类型");
        }
        if ("ML".equals(sysEntitlement.getEntType())) {
            if (StringUtils.isEmpty(sysEntitlement.getMenuIcon())) {
                throw new BizException("菜单图标不能为空");
            }
            if (StringUtils.isEmpty(sysEntitlement.getMenuUri())) {
                throw new BizException("路由地址不能为空");
            }
        }
        if (StringUtils.isEmpty(sysEntitlement.getPid())) {
            throw new BizException("请选择上级菜单");
        }
        if (ObjectUtils.isEmpty(sysEntitlement.getEntSort())) {
            throw new BizException("请输入排序");
        }
        if (ObjectUtils.isEmpty(sysEntitlement.getSysType())) {
            throw new BizException("请选择所属系统");
        }

        List<SysEntitlement> sysEntitlements = baseMapper.selectList(SysEntitlement.gw().eq(SysEntitlement::getEntId, sysEntitlement.getEntId())
                .eq(SysEntitlement::getSysType, sysEntitlement.getSysType()));
        if (ObjectUtils.isNotEmpty(sysEntitlements)) {
            throw new BizException("路由ID已存在");
        }

        sysEntitlement.setCreatedAt(new Date());
        sysEntitlement.setUpdatedAt(new Date());
        return baseMapper.insert(sysEntitlement);
    }

}
